package com.jh.user.controller;

import cn.jh.common.exception.ServiceException;
import cn.jh.common.utils.ResultWrap;
import com.jh.user.business.FastSilverPicPathBusiness;
import com.jh.user.business.UserBusiness;
import com.jh.user.business.UserLoginRegisterBusiness;
import com.jh.user.pojo.*;
import com.jh.user.repository.FastSilverPicPathRepository;
import com.jh.user.repository.UserRoleRepository;
import com.jh.user.util.CommonsUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author zhugu
 * 2018/11/29 9:04
 */
@RestController
public class FastSilverPicPathController {
    public static final Logger log = LoggerFactory.getLogger(FastSilverPicPathController.class);

    private final FastSilverPicPathBusiness fastSilverPicPathBusiness;
    private final UserLoginRegisterBusiness userLoginRegisterBusiness;
    private final UserRoleRepository userRoleRepository;
    private final FastSilverPicPathRepository fastSilverPicPathRepository;

    @Autowired
    FastSilverPicPathController(FastSilverPicPathBusiness fastSilverPicPathBusiness,
                                UserLoginRegisterBusiness userLoginRegisterBusiness,
                                UserRoleRepository userRoleRepository,
                                FastSilverPicPathRepository fastSilverPicPathRepository) {
        this.fastSilverPicPathBusiness = fastSilverPicPathBusiness;
        this.userLoginRegisterBusiness = userLoginRegisterBusiness;
        this.userRoleRepository = userRoleRepository;
        this.fastSilverPicPathRepository = fastSilverPicPathRepository;
    }

    /**
     * APP/管理后台：快银图片上传
     * userId
     * merchantHeadPic       门头照
     * merchantChechPic      收银台照
     * otherPhotoThreePic    经营场所照
     * identityFacePic       法人身份证正面照
     * identityBackPic       法人身份证反面照
     * businessCardPic       开户许可证照
     * businessPic           营业执照照
     * identityFaceCopyPic   结算人身份证正面照
     * identityBackCpoyPic   结算人身份证反面照
     * identityBodyPic       手持身份证照片
     * otherPhotoFourPic     非法人对私授权函
     * otherPhotoTwoPic      租房合同
     * otherPhotoPic         其他资料
     *
     * firstPhotoPic         内部前台照片
     * bankFrontPhotoPic     银行卡正面照片
     * bankReversePhotoPic   银行卡反面照片
     * mchScenePhotoPic      商户场景照片
     * relationshipPhotoPic  商户总分店关系证明
     * merchantPhotoPic      商户增值协议
     * @return
     */
    @PostMapping(value = "/v1.0/user/FastSilverPicPath/uploadPics")
    public Object uploadPics(FastSilverPicPathRequest fsppRequest) {
        try {
            FastSilverPicPathResponse fastSilverPicPathView = fastSilverPicPathBusiness.uploadPics(fsppRequest);
            return CommonsUtil.returnSuccess(fastSilverPicPathView, "图片上传成功！");
        } catch (ServiceException e) {
            e.printStackTrace();
            return CommonsUtil.returnError(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonsUtil.returnError("服务器异常，请稍后再试！");
        }
    }

    /**
     * 查询快银图片上传接口
     * @param userId
     * @return
     */
    @GetMapping(value = "/v1.0/user/fast_silver/query")
    public Object queryUplod(@RequestParam(required = true)long userId) {
        try {
            FastSilverPicPath path =fastSilverPicPathRepository.queryByUserId(userId);
            if (path == null) {
                return CommonsUtil.returnError("该用户未上传图片");
            }
            return CommonsUtil.returnSuccess(path);
        } catch (Exception e) {
            return CommonsUtil.returnError("同步查询失败！");
        }
    }


    @GetMapping(value = "/v1.0/user/fast_silver/user")
    public Object queryUser(@RequestParam(required = false) String phone) {
        User user = userLoginRegisterBusiness.queryUserByPhone(phone);
        return CommonsUtil.returnSuccess(user);
    }

    @GetMapping(value = "/v1.0/user/fast_silver/userId")
    public Object queryUserId(@RequestParam(required = false) long userId,
                              @RequestParam(required = false) String phone
                              ) {
        User user = new User();
        if (StringUtils.isNotBlank(phone)) {
            user = userLoginRegisterBusiness.queryUserByPhone(phone);
        } else {
            user = userLoginRegisterBusiness.queryUserById(userId);
            UserRole userRole = userRoleRepository.queryUserRole(user.getId());
            user.setGrade(String.valueOf(userRole.getRoleId()));
        }
        return CommonsUtil.returnSuccess(user);
    }

    /**
     * 查询图片快银上传信息
     * @param userId
     * @return
     */
    @GetMapping(value = "/v1.0/user/FastSilverPicPath/findFsppList")
    public Object findFsppList(long userId) {
        try {
            FastSilverPicPathResponse fsppResponse = fastSilverPicPathBusiness.findFsppList(userId);
            return ResultWrap.ok("查询成功！", fsppResponse);
        } catch (ServiceException e) {
            e.printStackTrace();
            return CommonsUtil.returnError(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return CommonsUtil.returnError("服务器异常，请稍后再试！");
        }
    }
}
